home *** CD-ROM | disk | FTP | other *** search
/ PC User 2003 December / Australian PC User - December 2003 (CD2).iso / software / apps / files / dwmx2k4.exe / Disk1 / data1.cab / Configuration_En / Objects / JSP / JSPPageEncoding.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  2.7 KB  |  91 lines

  1. // Copyright 1999, 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved.
  2.  
  3. //---------------     API FUNCTIONS    ---------------
  4.  
  5. function isDOMRequired() {
  6.     return false;
  7. }
  8.  
  9.  
  10. function objectTag() {
  11.   var dom = dw.getDocumentDOM();
  12.   var encoding = "";
  13.   if( dom )
  14.         encoding = dom.getCharSet();
  15.   var requestEncodingStr = "request.setCharacterEncoding(\"" + encoding + "\");";
  16.   
  17.   //for text view, just drop the code at the IP, assume the coder is putting it in the correct place
  18.   if( dw.getFocus() == 'textView' )
  19.   {
  20.       return requestEncodingStr;
  21.   }
  22.   else if (dom)
  23.   {
  24.     
  25.     if (encoding)
  26.     {
  27.       var docStr = dom.documentElement.outerHTML;
  28.       var pageDirectiveEnd = 0;
  29.       var pageDirectiveStart = 0;
  30.       var selectionStart = 0;
  31.       var selectionEnd = 0;
  32.       var newDocStr = "";
  33.       var updateCode = false;
  34.  
  35.       //first try to see if this tag is already here and up to date
  36.       if( -1 < docStr.search( /request.setCharacterEncoding\(["'][\w-]*["']\)/i ))
  37.       {
  38.             dom.serverModel.updatePageDirective(dom);
  39.             docStr = dom.documentElement.outerHTML;
  40.             selectionStart = docStr.search( /request.setCharacterEncoding\(["'][\w-]*["']\)/i );
  41.             selectionEnd = docStr.indexOf( ")", selectionStart ) +1;
  42.       }
  43.       else
  44.       {  
  45.           //try to add this after the page directive
  46.           pageDirectiveStart = docStr.search( /<%@\s*Page/i );
  47.           if( pageDirectiveStart == -1 )
  48.           {
  49.               //we didn't find the page directive, just add it to the top of the document 
  50.               newDocStr = requestEncodingStr + docStr;
  51.               selectionEnd = requestEncodingStr.length;
  52.               updateCode = true;
  53.           }
  54.           else
  55.           {
  56.                 pageDirectiveEnd =  docStr.indexOf("%>", pageDirectiveStart) + 2;
  57.               if(pageDirectiveEnd > 1 && pageDirectiveEnd > pageDirectiveStart)
  58.               {
  59.                   //include any trailing whitespace in the page directive
  60.                   var whiteSpace = " \f\n\r\t\v";
  61.                   if( whiteSpace.indexOf( docStr.charAt( pageDirectiveEnd +1 ) ) != -1 )
  62.                   {
  63.                       pageDirectiveEnd++;    
  64.                   }
  65.                   
  66.                   while( whiteSpace.indexOf( docStr.charAt( pageDirectiveEnd ) ) != -1 )
  67.                   {
  68.                     pageDirectiveEnd++;
  69.                   }
  70.                   
  71.                   newDocStr = docStr.substring( 0 , pageDirectiveEnd ) + 
  72.                               "<% " + requestEncodingStr + " %>\n" +
  73.                               docStr.substring( pageDirectiveEnd , docStr.length );
  74.                   updateCode = true;
  75.                   selectionStart = pageDirectiveEnd;
  76.                   selectionEnd = selectionStart + requestEncodingStr.length;
  77.               }
  78.                   
  79.           }
  80.       }
  81.  
  82.       if( updateCode )
  83.         dom.documentElement.outerHTML = newDocStr;
  84.       
  85.       //put the focus to the code view, so the user know we did something
  86.       dom.source.setSelection(selectionStart, selectionEnd);
  87.     }
  88.   }
  89.   
  90. }
  91.